package com.szhmxxkj.job;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsResponse.Record;
import com.szhmxxkj.common.tools.core.support.CollectionKit;
import com.szhmxxkj.common.tools.core.support.DateTimeKit;
import com.szhmxxkj.common.tools.core.util.DateUtil;
import com.szhmxxkj.config.service.DynamicDnsService;
import com.szhmxxkj.config.service.impl.DynamicDnsServiceImpl;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
@DisallowConcurrentExecution//这样第二个任务会往后推迟，只有在第一个任务执行完成后才会执行第二个任
public class UpdateDomainRecordJob extends QuartzJobBean {
	
	@Autowired
	private DynamicDnsService dynamicDnsService;
	
	@Value("${job.rrs}")
	private String rrs;
	
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		// TODO Auto-generated method stub
		log.info("更新dns解析开始执行,开始时间{}",DateUtil.getTime());
		update();
	}
	
	private static void ping() throws IOException {
		Process process = Runtime.getRuntime().exec("ping gdgl.szhmxxkj.com");
		InputStream inputStream = process.getInputStream();
		InputStreamReader reader=new InputStreamReader(inputStream,"gbk");
	    BufferedReader reader2=new BufferedReader(reader);
	    String readLine =null;
	    int i=0;
	    while((readLine= reader2.readLine())!=null && i<10) {
	       System.out.println(readLine);
	       i++;
	    }
	    process.destroyForcibly();
	    
	}
	public static void main(String[] args) throws IOException {
		//ping();
		System.out.println(DateUtil.getTime());
	}
	/**
	 * 更新ip地址
	 */
	private void update() {
		String domainName="szhmxxkj.com";
		List<Record> domainRecords = dynamicDnsService.describeDomainRecords(domainName);
		String[] rrs=this.rrs.split(",");
		String newIP=DynamicDnsServiceImpl.getIp();
		log.info("获取最新ip地址是:{}",newIP);
		domainRecords.stream().filter(e->CollectionKit.contains(rrs, e.getRR()))
		.forEach(e->{
			String oldIp=e.getValue();
			log.info("域名{}的解析地址是{}",e.getRR(),oldIp);
			if(!oldIp.equals(newIP)) {
				log.info("开始更新{}解析的ip地址为:{}",e.getRR(),newIP);
				dynamicDnsService.updateDomainRecord(e.getRecordId(), e.getRR(),newIP);
			}else {
				log.info("ip地址相同，无须更新");
			}
		});
		/*domainRecords.forEach(e->{
			String RR = e.getRR();
			String recordId = e.getRecordId();
			if(CollectionKit.contains(rrs, RR)) {
				dynamicDnsService.updateDomainRecord(recordId, RR);
			}
		});*/
	}

}
